//
//  SQStatusViewModel.swift
//  SwiftWeibo
//
//  Created by 苏强 on 16/12/5.
//  Copyright © 2016年 suqianghotel. All rights reserved.
//

import Foundation
import YYModel

class SQStatusViewModel : CustomStringConvertible{
    
    var status : SQStatus
    
    var authImage: UIImage?
    
    var vipImage: UIImage?
    
    var timeString: String?
    
    var sourceString: String?
    
    var retweetCountString: String?
    var commentCountString: String?
    var likeCountString: String?
    
    var pictureSize = CGSize()
    
    var picURLs:[SQStatusPicture]?
    
    var statusText: String?
    
    var repostStatusText: String?
    
    var rowHeight: CGFloat = 0
    
    var reuseID: String?
    
    init(status: SQStatus) {
        
        self.status = status
        
        self.statusText = status.text
        
        self.retweetCountString = countString(with: status.reposts_count, placdeholder: "转发")
        
        self.commentCountString = countString(with: status.comments_count, placdeholder: "评论")
        
        self.likeCountString = countString(with: status.attitudes_count, placdeholder: "赞")
        
        /// 有转发微博时，只取得转发微博配图
        if status.retweeted_status == nil {
            picURLs = status.pic_urls
            reuseID = SQStatusCellID
        }
        else
        {
            picURLs = status.retweeted_status?.pic_urls
            
            let username = status.retweeted_status?.user?.screen_name ?? ""
            let text = status.retweeted_status?.text ?? ""
            
            repostStatusText = "@\(username): \(text)"
            
            reuseID = SQRepostStatusCellID
        }
        
        self.pictureSize = calculatePicturesViewSize(with: picURLs?.count)
        
        calculateRowHight()
        
        
    }
    
    
    private func calculateRowHight()
    {
        // 1. 分隔线高度 8
        // 2. 边距 + 头像高度 34
        // 3. 边距 + 文字高度
        // 4. 原创  配图高度(无默认12)
        //    转发  边距 + 边距 + 转发文字高度
        //         配图高度(无默认12)
        //
        // 5. ToolBar 高度 35
        
        var height:CGFloat = 0
        let margin = SQStatusCellOutterMargin
        let contentW = SQStatusViewWidth
        let viewSize = CGSize(width: contentW, height: CGFloat.greatestFiniteMagnitude)
        let attrs = [NSFontAttributeName:UIFont.systemFont(ofSize: 16)]
        
        // 1. 分隔线高度 8
        height += CGFloat(8)
        
        // 2. 边距 + 头像高度 34
        height += margin + CGFloat(34)
        
        // 3. 边距 + 文字高度
        height += margin
        height += self.statusText?.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], attributes: attrs, context: nil).size.height ?? 0
        
        // 4. 原创  配图高度(无默认12)
        //    转发  边距 + 转发文字高度
        //         配图高度(无默认12)
        height += pictureSize.height
        
        if self.repostStatusText != nil {
            height += margin
            height += (margin + self.repostStatusText!.boundingRect(with: viewSize, options: [.usesLineFragmentOrigin], attributes: attrs, context: nil).size.height)
        }
        
        
        // 5. ToolBar 高度 35
        height += CGFloat(35)
        
        rowHeight = height
        
    }
    
    
    private func calculatePicturesViewSize(with picNum:Int?) -> CGSize
    {
        var height = SQStatusCellOutterMargin
        
        if let picNum = picNum
        {
            if picNum != 0
            {
                let row = CGFloat((picNum - 1) / 3 + 1)
                
                height += row * SQStatusPictureViewWidth
                height += (row - 1) * SQStatusPictureViewInnerMargin
                height += SQStatusCellOutterMargin

            }
            
        }
    
        
        return CGSize(width:SQStatusViewWidth, height:height)
    }
    
    
    
    private func countString(with number:Int?, placdeholder:String) -> String
    {
        guard let number = number else {
            return placdeholder
        }
        
        if number == 0{
            return placdeholder
        }
        
        if number >= 10000 {
            return String(format: "%.02f 万", Double(number) / 10000)
        }
        
        return "\(number)"
    }
    
    var description: String{
        return status.description
    }
    
}
